package com.yztstudy.Controller;

import com.yztstudy.Service.ProductService;
import com.yztstudy.entity.Product;
import com.yztstudy.param.ProductCollectParam;
import com.yztstudy.param.ProductIdParam;
import com.yztstudy.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物车调用商品服务的表示层
 */

@RestController
@RequestMapping("/product")
public class ProductCartController {

    @Autowired
    private ProductService productService;


    /**
     * TODO 购物车中的商品详情
     * @param productIdParam 商品ID
     * @param result 校验后的结果
     * @return
     */
    @PostMapping("/cart/detail")
    public Product cartDetail(@RequestBody @Validated ProductIdParam productIdParam, BindingResult result)
    {

        // TODO 1.如果校验失败
        if (result.hasErrors())
        {
            return null;
        }

        // TODO 2.校验成功就查询详情，并返回
        R detail = productService.detail(productIdParam);
        Product product = (Product) detail.getData();
        return product;
    }


    /**
     * TODO 根据商品ID集合来获取多个商品
     * @param productCollectParam 商品ID集合
     * @param result 校验后的结果
     * @return
     */
    @PostMapping("/cart/list")
    public List<Product> cartList(@RequestBody @Validated ProductCollectParam productCollectParam, BindingResult result)
    {
        // TODO 1.如果校验失败
        if (result.hasErrors())
        {
            return new ArrayList<Product>();
        }

        // TODO 2.校验成功就查询所有符合条件的商品
        return productService.cartList(productCollectParam);
    }
}
